<?php

namespace JLPay\Services\Ledger\Merchconf;

use JLPay\Services\BaseService;
use JLPay\Models\Ledger\Merchconf\MerchRevQueryRequest;
use JLPay\Models\Ledger\Merchconf\MerchRevQueryResponse;
use JLPay\Models\Ledger\Merchconf\MerchRevUnbindRequest;
use JLPay\Models\Ledger\Merchconf\MerchRevUnbindResponse;
use JLPay\Models\Ledger\Merchconf\MerchFileUploadRequest;
use JLPay\Models\Ledger\Merchconf\MerchFileUploadResponse;
use JLPay\Models\Ledger\Merchconf\MerchIssueOpenRequest;
use JLPay\Models\Ledger\Merchconf\MerchIssueOpenResponse;
use JLPay\Models\Ledger\Merchconf\MerchIssueQueryRequest;
use JLPay\Models\Ledger\Merchconf\MerchIssueQueryResponse;
use JLPay\Models\Ledger\Merchconf\MerchIssueUpdateRequest;
use JLPay\Models\Ledger\Merchconf\MerchIssueUpdateResponse;
use JLPay\Models\Ledger\Merchconf\MerchRevSettleUpdateRequest;
use JLPay\Models\Ledger\Merchconf\MerchRevSettleUpdateResponse;
use JLPay\Models\Ledger\Merchconf\MerchRevBindQueryRequest;
use JLPay\Models\Ledger\Merchconf\MerchRevBindQueryResponse;
use JLPay\Models\Ledger\Merchconf\MerchRevBindRequest;
use JLPay\Models\Ledger\Merchconf\MerchRevBindResponse;
use JLPay\Models\Ledger\Merchconf\MerchRevOpenRequest;
use JLPay\Models\Ledger\Merchconf\MerchRevOpenResponse;

/**
 * 商户配置服务类
 */
class MerchconfService extends BaseService
{
    /**
     * 获取API URL
     *
     * @param string $requestClass 请求类名
     * @param string|null $apiPath 接口路径
     * @return string 完整的API URL
     */
    public function getApiUrl(string $requestClass, ?string $apiPath = null): string
    {
        $baseUrl = $this->config->getUrl();

        if ($apiPath) {
            return $baseUrl . $apiPath;
        }

        // 直接使用请求类中定义的完整API路径
        if (defined($requestClass . '::API_URL')) {
            $apiUrl = constant($requestClass . '::API_URL');
            return $baseUrl . $apiUrl;
        }

        // 如果没有定义API_URL，使用默认路径
        return $baseUrl . '/open/ledger/merchconf';
    }

    /**
     * 查询商户分账信息
     *
     * @param MerchRevQueryRequest $request 请求对象
     * @return MerchRevQueryResponse 响应对象
     */
    public function queryMerchRev(MerchRevQueryRequest $request): MerchRevQueryResponse
    {
        return $this->post($request, MerchRevQueryResponse::class);
    }

    /**
     * 解绑商户分账
     *
     * @param MerchRevUnbindRequest $request 请求对象
     * @return MerchRevUnbindResponse 响应对象
     */
    public function unbindMerchRev(MerchRevUnbindRequest $request): MerchRevUnbindResponse
    {
        return $this->post($request, MerchRevUnbindResponse::class);
    }

    /**
     * 上传商户文件
     *
     * @param MerchFileUploadRequest $request 请求对象
     * @return MerchFileUploadResponse 响应对象
     */
    public function uploadMerchFile(MerchFileUploadRequest $request): MerchFileUploadResponse
    {
        return $this->post($request, MerchFileUploadResponse::class);
    }

    /**
     * 开通商户分账
     *
     * @param MerchIssueOpenRequest $request 请求对象
     * @return MerchIssueOpenResponse 响应对象
     */
    public function openMerchIssue(MerchIssueOpenRequest $request): MerchIssueOpenResponse
    {
        return $this->post($request, MerchIssueOpenResponse::class);
    }

    /**
     * 查询商户分账
     *
     * @param MerchIssueQueryRequest $request 请求对象
     * @return MerchIssueQueryResponse 响应对象
     */
    public function queryMerchIssue(MerchIssueQueryRequest $request): MerchIssueQueryResponse
    {
        return $this->post($request, MerchIssueQueryResponse::class);
    }

    /**
     * 更新商户分账
     *
     * @param MerchIssueUpdateRequest $request 请求对象
     * @return MerchIssueUpdateResponse 响应对象
     */
    public function updateMerchIssue(MerchIssueUpdateRequest $request): MerchIssueUpdateResponse
    {
        return $this->post($request, MerchIssueUpdateResponse::class);
    }

    /**
     * 更新商户分账结算
     *
     * @param MerchRevSettleUpdateRequest $request 请求对象
     * @return MerchRevSettleUpdateResponse 响应对象
     */
    public function updateMerchRevSettle(MerchRevSettleUpdateRequest $request): MerchRevSettleUpdateResponse
    {
        return $this->post($request, MerchRevSettleUpdateResponse::class);
    }

    /**
     * 查询商户分账绑定
     *
     * @param MerchRevBindQueryRequest $request 请求对象
     * @return MerchRevBindQueryResponse 响应对象
     */
    public function queryMerchRevBind(MerchRevBindQueryRequest $request): MerchRevBindQueryResponse
    {
        return $this->post($request, MerchRevBindQueryResponse::class);
    }

    /**
     * 绑定商户分账
     *
     * @param MerchRevBindRequest $request 请求对象
     * @return MerchRevBindResponse 响应对象
     */
    public function bindMerchRev(MerchRevBindRequest $request): MerchRevBindResponse
    {
        return $this->post($request, MerchRevBindResponse::class);
    }

    /**
     * 开通商户分账
     *
     * @param MerchRevOpenRequest $request 请求对象
     * @return MerchRevOpenResponse 响应对象
     */
    public function openMerchRev(MerchRevOpenRequest $request): MerchRevOpenResponse
    {
        return $this->post($request, MerchRevOpenResponse::class);
    }
} 